/*     */ package com.zimbra.cs.mailbox.alerts;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.AlarmData;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxListener;
/*     */ import com.zimbra.cs.mailbox.MailboxListener.ChangeNotification;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.MailboxManager.FetchMode;
/*     */ import com.zimbra.cs.mailbox.ScheduledTaskManager;
/*     */ import com.zimbra.cs.mailbox.calendar.Alarm;
/*     */ import com.zimbra.cs.mailbox.calendar.ZAttendee;
/*     */ import com.zimbra.cs.session.PendingModifications;
/*     */ import com.zimbra.cs.session.PendingModifications.Change;
/*     */ import com.zimbra.cs.session.PendingModifications.ModificationKey;
/*     */ import java.util.Date;
/*     */ import java.util.EnumSet;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CalItemReminderService
/*     */   extends MailboxListener
/*     */ {
/*     */   public void notify(MailboxListener.ChangeNotification notification)
/*     */   {
/*  46 */     Account account = notification.mailboxAccount;
/*  47 */     if (notification.mods.created != null) {
/*  48 */       for (Map.Entry<PendingModifications.ModificationKey, MailItem> entry : notification.mods.created.entrySet()) {
/*  49 */         MailItem item = (MailItem)entry.getValue();
/*  50 */         if ((item instanceof CalendarItem)) {
/*  51 */           ZimbraLog.scheduler.debug("Handling creation of calendar item (id=%s,mailboxId=%s)", new Object[] { Integer.valueOf(item.getId()), Integer.valueOf(item.getMailboxId()) });
/*     */           
/*  53 */           scheduleNextReminders((CalendarItem)item, true, true);
/*     */         }
/*     */       }
/*     */     }
/*  57 */     if (notification.mods.modified != null) {
/*  58 */       for (Map.Entry<PendingModifications.ModificationKey, PendingModifications.Change> entry : notification.mods.modified.entrySet()) {
/*  59 */         PendingModifications.Change change = (PendingModifications.Change)entry.getValue();
/*  60 */         if ((change.what instanceof CalendarItem)) {
/*  61 */           CalendarItem calItem = (CalendarItem)change.what;
/*  62 */           ZimbraLog.scheduler.debug("Handling modification of calendar item (id=%s,mailboxId=%s)", new Object[] { Integer.valueOf(calItem.getId()), Integer.valueOf(calItem.getMailboxId()) });
/*     */           
/*  64 */           boolean calItemCanceled = false;
/*     */           try {
/*  66 */             if (((change.why & 0x100) != 0) && (calItem.inTrash())) {
/*  67 */               calItemCanceled = true;
/*     */             }
/*     */           } catch (ServiceException e) {
/*  70 */             ZimbraLog.scheduler.error("Error in fetching calendar item's folder", e);
/*     */           }
/*     */           
/*  73 */           if ((cancelExistingReminders(calItem)) && (!calItemCanceled))
/*  74 */             scheduleNextReminders(calItem, true, true);
/*     */         }
/*     */       }
/*     */     }
/*  78 */     if (notification.mods.deleted != null) {
/*  79 */       for (Map.Entry<PendingModifications.ModificationKey, PendingModifications.Change> entry : notification.mods.deleted.entrySet()) {
/*  80 */         MailItem.Type type = (MailItem.Type)((PendingModifications.Change)entry.getValue()).what;
/*  81 */         if ((type == MailItem.Type.APPOINTMENT) || (type == MailItem.Type.TASK)) {
/*  82 */           Mailbox mbox = null;
/*     */           try {
/*  84 */             mbox = MailboxManager.getInstance().getMailboxByAccount(account, MailboxManager.FetchMode.DO_NOT_AUTOCREATE);
/*     */           }
/*     */           catch (ServiceException e) {
/*  87 */             ZimbraLog.scheduler.error("Error looking up the mailbox of account %s", account.getId(), e);
/*     */           }
/*  89 */           if (mbox != null) {
/*  90 */             cancelExistingReminders(((PendingModifications.ModificationKey)entry.getKey()).getItemId().intValue(), mbox.getId());
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static boolean cancelExistingReminders(CalendarItem calItem)
/*     */   {
/* 104 */     return cancelExistingReminders(calItem.getId(), calItem.getMailboxId());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static boolean cancelExistingReminders(int calItemId, int mailboxId)
/*     */   {
/*     */     try
/*     */     {
/* 116 */       ScheduledTaskManager.cancel(CalItemEmailReminderTask.class.getName(), "emailReminderTask" + Integer.toString(calItemId), mailboxId, false);
/*     */       
/*     */ 
/*     */ 
/* 120 */       ScheduledTaskManager.cancel(CalItemSmsReminderTask.class.getName(), "smsReminderTask" + Integer.toString(calItemId), mailboxId, false);
/*     */ 
/*     */     }
/*     */     catch (ServiceException e)
/*     */     {
/* 125 */       ZimbraLog.scheduler.warn("Canceling reminder tasks failed", e);
/* 126 */       return false;
/*     */     }
/* 128 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static void scheduleNextReminders(CalendarItem calItem, boolean email, boolean sms)
/*     */   {
/*     */     try
/*     */     {
/* 140 */       CalendarItem.AlarmData alarmData = calItem.getNextEmailAlarm();
/* 141 */       if (alarmData == null)
/* 142 */         return;
/* 143 */       boolean emailAlarmExists = true;
/* 144 */       boolean smsAlarmExists = false;
/* 145 */       Alarm emailAlarm = alarmData.getAlarm();
/* 146 */       List<ZAttendee> recipients = emailAlarm.getAttendees();
/* 147 */       String defaultEmailAddress; String defaultDeviceAddress; if ((recipients != null) && (!recipients.isEmpty())) {
/* 148 */         emailAlarmExists = false;
/* 149 */         Account acct = calItem.getAccount();
/* 150 */         defaultEmailAddress = acct.getPrefCalendarReminderEmail();
/* 151 */         defaultDeviceAddress = acct.getCalendarReminderDeviceEmail();
/* 152 */         for (ZAttendee recipient : recipients) {
/* 153 */           if (recipient.getAddress().equals(defaultEmailAddress)) {
/* 154 */             emailAlarmExists = true;
/*     */           }
/* 156 */           if (recipient.getAddress().equals(defaultDeviceAddress)) {
/* 157 */             smsAlarmExists = true;
/*     */           }
/*     */         }
/*     */       }
/* 161 */       if ((emailAlarmExists) && (email)) {
/* 162 */         scheduleReminder(new CalItemEmailReminderTask(), calItem, alarmData);
/*     */       }
/* 164 */       if ((smsAlarmExists) && (sms)) {
/* 165 */         scheduleReminder(new CalItemSmsReminderTask(), calItem, alarmData);
/*     */       }
/*     */     } catch (ServiceException e) {
/* 168 */       ZimbraLog.scheduler.error("Error in scheduling reminder task", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void scheduleReminder(CalItemReminderTaskBase reminderTask, CalendarItem calItem, CalendarItem.AlarmData alarmData)
/*     */     throws ServiceException
/*     */   {
/* 175 */     reminderTask.setMailboxId(calItem.getMailboxId());
/* 176 */     reminderTask.setExecTime(new Date(alarmData.getNextAt()));
/* 177 */     reminderTask.setProperty("calItemId", Integer.toString(calItem.getId()));
/* 178 */     reminderTask.setProperty("invId", Integer.toString(alarmData.getInvId()));
/* 179 */     reminderTask.setProperty("compNum", Integer.toString(alarmData.getCompNum()));
/* 180 */     reminderTask.setProperty("nextInstStart", Long.toString(alarmData.getNextInstanceStart()));
/* 181 */     ScheduledTaskManager.schedule(reminderTask);
/*     */   }
/*     */   
/*     */   public Set<MailItem.Type> registerForItemTypes()
/*     */   {
/* 186 */     return EnumSet.of(MailItem.Type.APPOINTMENT, MailItem.Type.TASK);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/alerts/CalItemReminderService.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */